package HW2_3.Firefox_page;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import HW2_3.pages.InternalPage;
import HW2_3.pages.LoginPage;
import HW2_3.pages.UserPage;





public class b_new_user {
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	private LoginPage loginPage;
	private UserPage userPage;
	private InternalPage internalPage;
	@Before
	public void setUp() throws Exception {
		
		 ProfilesIni allProfiles = new ProfilesIni(); 
	     FirefoxProfile profile = allProfiles.getProfile("default"); 
	     driver = new FirefoxDriver(profile); 
	     
	     baseUrl = "http://localhost/tools/";
	}

	@Test
	public void a_new_user() throws Exception {
		GoToMainPage();
				assertTrue("Not Main Page", loginPage.isOnMainPage());
		LoginAs("admin", "admin");
				assertTrue("LoginAs error", internalPage.isLoginOk()); 
		GoToUserManager();
				assertTrue("Not Manager Page", userPage.IsOnManagerPage());
		AddNewUser("kalinka", "kalinka955@mail.ru", "kalinka", "Admin");
				assertTrue ("Not create new user", userPage.isNewUserOk());
		logout();
				assertTrue("Not logout", loginPage.isOnMainPage());
	}

	
	private void logout() {
		internalPage.menuLogoutLink.click();
		driver.switchTo().alert().accept();
	}

	private void AddNewUser(String userName, String email, String password, String role) {
		userPage.usernameField.clear();
		userPage.usernameField.sendKeys(userName);
		userPage.emailField.clear();
		userPage.emailField.sendKeys(email);
		userPage.passwordField.clear();
		userPage.passwordField.sendKeys(password);
		userPage.password2Field.clear();
		userPage.password2Field.sendKeys(password);
		//userPage.roleField.(role);
		userPage.saveButton.click();
	}

	private void GoToUserManager() {
		internalPage.menuUserManagementLink.click();
		}
	
	
	private void LoginAs(String username, String password) {
		loginPage.userNameField.clear();
		loginPage.userNameField.sendKeys(username);
		loginPage.passwordField.clear();
		loginPage.passwordField.sendKeys(password);
		loginPage.loginButton.click();
	}
	
	private void GoToMainPage() {
		driver.get(baseUrl + "/php4dvd/");
		loginPage = PageFactory.initElements(driver, LoginPage.class);
		userPage = PageFactory.initElements(driver, UserPage.class);
		internalPage = PageFactory.initElements(driver,InternalPage.class);
	}

		@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}
